//订单确认页面


Page({
  //当前页面数据
  data: {
    product: {},
    nums:0,
    shuxingid:'',
    baseurl: 'https://wukongweixiu.cn/',
    multiArray: [
      ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22','23'], 
      ['0', '30']
      ],
    date_d:'',

    img0: '../../img/20181201173806.png',
    img1: '../../img/20181201173806.png',
    img2: '../../img/20181201173806.png',
    x: '',
    img: [],
    datatime_h:'00时',
    datatime_s: '00分',
    cartIds: '', //购物车ID
    productData: { }, //货品信息
    totalPrice: 0, //总价
    isAddress: false, //是否有地址
    Addressid: 0, //地址id
    Address: {}, //地址id
    id:0,//是否是单独购买的产品id
    mun:0,//单独购买的数量
    areaId: 0, //地区ID
    area: '', //地区信息
    // address: '', //地址信息
    name: '', //收货人姓名
    mobile: '', //收货人电话
    order_mun:'',
    date:'',
    datatime: '00:00:00',
    buyerMessage:'',
    xieyi:0,
    
  },

  pickerTap: function (e) {
    
    var that = this;
    var multiArray = that.data.multiArray;
    var v ='';
    var v1='';
    var times = e.detail.value;
    if (times[1] ==1){
      v = multiArray[0][times[0]] + ':' + '30' + ':' + '00';
      v1 = '30分'
    }else{
      v = times[0] + ':' +'00'+':'+'00';
      v1 = '0分'
    }
    that.setData({
      datatime: v,
      datatime_h: multiArray[0][times[0]]+'时',
      datatime_s: v1
    })
  },
  onLoad: function (options) {
    console.log(options)
    var page = this;
    var id = options.id;
    var mun = options.mun
    var shuxingid = options.shuxingid
    if (typeof (id) != 'undefined' && typeof (mun) != 'undefined')
    page.setData({
      id: id,
      mun: mun,
      shuxingid: shuxingid
    })
    
   
  },
  chooseImageTap: function (e) {
    let _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        var x = e.target.dataset.id;

        _this.setData({
          x: x,
        })
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            _this.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            _this.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function (type) {
    let _this = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        wx.uploadFile({
          url: 'https://wukongweixiu.cn/index/order/uploadImg',
          filePath: res.tempFilePaths[0],
          name: 'file',
          success: function (re) {
            var jsonStr = re.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");//重点
              var jj = JSON.parse(jsonStr);
            }
            var x = _this.data.x;
            var img = _this.data.img;
            img[x] = jj.data.src;
            if (jj.code == 1) {
              if (x == 0) {
                _this.setData({
                  img0: 'https://wukongweixiu.cn' + jj.data.src,
                  img
                })
              } else if (x == 1) {
                _this.setData({
                  img1: 'https://wukongweixiu.cn' + jj.data.src,
                  img
                })
              } else {
                _this.setData({
                  img2: 'https://wukongweixiu.cn' + jj.data.src,
                  img
                })
              }
              wx.showToast({
                title: '已提交发布！',
                duration: 3000
              });
            }
          }
        })
      }
    })
  },
  onShow:function(){
    
    var that = this;
    var date = that.getNowFormatDate();
    that.setData({
      date: date,
      date_d: date
    })
    var goodsid = that.data.id;
    var data = {};
    if (goodsid){
       var mun = that.data.mun;
        data={
          unionid: getApp().globalData.userid,
          goodsid: goodsid,
          mun: mun,
          shuxingid: that.data.shuxingid
        }
    }else{
      data = {
        unionid: getApp().globalData.userid
      }
    }

    wx.request({
      url: 'https://wukongweixiu.cn/index/Defaults/getAll',
      data: data,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) { 
        
        var Addr = false; //是否有地址
        if (res.data.data.addr1 ) {
          Addr = true
        }
        that.setData({
          productData: res.data.data.data,
          totalPrice: res.data.data.total,
          isAddress: Addr,
          Address: res.data.data.falgaddr,
          Addressid: res.data.data.falgaddr.addr_id

        })
      
      }
    })
  },
  getNowFormatDate:function () {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if(month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
      var currentdate = year + seperator1 + month + seperator1 + strDate;
      return currentdate;
    },

  bindDateChange: function (e) {
    var that = this
    that.setData({
      date: e.detail.value
    })
   
  },

  
  buyerMessage: function (e) {
    var that = this
    that.setData({
      buyerMessage: e.detail.value
    })
    
  },

  //选择地址
  selectAddress: function () {
    let page = this;
    wx.navigateTo({
      url: '../addressList/addressList',
    });
    
  },
  xieyi: function (e){
      var that =this;
      var xieyi = that.data.xieyi;
   
    if (xieyi == 1){
        xieyi =0;
    }else{
      xieyi=1;
    }
      that.setData({
        xieyi: xieyi
      })
  },
  xieyi_jump:function(){
    wx.navigateTo({
      url: '../xieyijump/xieyijump',
    });
  },

  //立即支付生成订单
  payNow: function (e) {
    let page = this;
    var xieyi = page.data.xieyi;
    console.log(xieyi)
    if (xieyi != 1){
      wx.showToast({
        title: '请阅读用户协议',
        duration: 3000
      });
      return false;
    }



    var goodsid = page.data.id;
    var mun = page.data.mun;
    var data = {};
    if (goodsid && mun){
      data = {
        unionid: getApp().globalData.userid,
        userid: getApp().globalData.id,
        addr_id: page.data.Addressid,
        date: page.data.date + ' ' + page.data.datatime,
        remark: page.data.buyerMessage,
        shuxingid: page.data.productData[0]['shuxingid'],
        goodsid: goodsid,
        mun: mun,
        type: 1,
        img: page.data.img
      };
    }else{
      data = {
        unionid: getApp().globalData.userid,
        userid: getApp().globalData.id,
        addr_id: page.data.Addressid,
        date: page.data.date + ' ' + page.data.datatime,
        remark: page.data.buyerMessage,
        type: 1,
        img:page.data.img
      };
    }
   
   
    wx.request({
      url: 'https://wukongweixiu.cn/index/Order/setorder',
      data:data,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        if (res.data.code == 1){
          var datas = {
            order_mun: res.data.data.order_mun,
            totalPrice: page.data.totalPrice,
            type:1
          }
          wx.navigateTo({
            url: '../cashierDesk/cashierDesk?data=' + JSON.stringify(datas),
          });
        }else{

        }
        
      }
    })




    














  }


});